package com.ruoyi.front_desk.controller;
/**
 * @author 刘奥旸
 * @version 1.0
 */
import com.ruoyi.common.core.domain.R;
import com.ruoyi.front_desk.Result;
import com.ruoyi.front_desk.domain.FrontdeskNameCategory;
import com.ruoyi.front_desk.service.FrontdeskNameCategoryService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
/**
 * @author 刘奥旸
 * @version 1.0
 */
@RestController
public class FrontdeskNameCategoryController {
    @Resource
    public FrontdeskNameCategoryService frontdeskNameCategoryService;
    // 静态代码块，添加 BouncyCastle 提供的加密算法实现
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    // 定义算法名称和填充方式
    private static final String ALGORITHM_NAME = "SM4";
    private static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
    private static final int DEFAULT_KEY_SIZE = 128; // SM4 的密钥长度为 128 位

    /**
     * 生成 SM4 密钥
     * @return 生成的密钥字节数组
     * @throws Exception
     */
    public static byte[] generateKey() throws Exception {
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
        kg.init(DEFAULT_KEY_SIZE);
        SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 使用 SM4 加密数据
     * @param key 加密密钥
     * @param data 需要加密的数据
     * @return 加密后的字节数组
     * @throws Exception
     */
    public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING, BouncyCastleProvider.PROVIDER_NAME);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    /**
     * 使用 SM4 解密数据
     * @param key 解密密钥
     * @param data 需要解密的数据
     * @return 解密后的字节数组
     * @throws Exception
     */
    public static byte[] decrypt(byte[] key, byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING, BouncyCastleProvider.PROVIDER_NAME);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM_NAME);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }


    @GetMapping(value = "/exer")
    public Result exer  (@RequestParam("id") String id){
        try {
// 原始文本
        String plainText = "Hello, World!";
        byte[] plainTextBytes = plainText.getBytes();

        // 生成 SM4 密钥
        byte[] key = generateKey();
        System.out.println("SM4 Key: " + Hex.toHexString(key));

        // 加密
        byte[] encryptedBytes = encrypt(key, plainTextBytes);
        System.out.println("Encrypted: " + Hex.toHexString(encryptedBytes));

        // 解密
        byte[] decryptedBytes = decrypt(key, encryptedBytes);
        String decryptedText = new String(decryptedBytes);
        System.out.println("Decrypted: " + decryptedText);
    }catch (Exception e){};
      return null;
    }
    //首页导航栏
    @GetMapping(value = "/homepage/navigation")
    public Result navigation(@RequestParam("id") Integer id){
        List<FrontdeskNameCategory> frontdeskNameCategoryById = frontdeskNameCategoryService.getNameCategoryById(id);
        // 提取名称并返回
        List<String> names = new ArrayList<>();
        for (FrontdeskNameCategory category : frontdeskNameCategoryById) {
            names.add(category.getName());
        }
        // 使用增强 for 循环逐个打印 names 集合中的每个名称
        for (String name : names) {
            System.out.println(name);
        }
        return Result.success(frontdeskNameCategoryById);
    }
    //星期+日期
    @GetMapping(value = "/homepage/data")
    public Result daytime(){
        LocalDate currentDate = LocalDate.now();
        // 获取当前星期几
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.CHINESE);

        // 使用格式化器格式化当前日期
        String formattedDate = currentDate.format(formatter);

        String dayOfWeekInChinese = getDayOfWeekInChinese(dayOfWeek);
        // 打印当前日期和星期几
       // System.out.println("当前日期: " + formattedDate);
       // System.out.println("今天是: " + dayOfWeekInChinese);
        // 构建返回结果
        Map<String, String> response = new HashMap<>();
        response.put("date", formattedDate);
        response.put("dayOfWeek", dayOfWeekInChinese);
        return Result.success(response);

    }
    //辅助方法日期变中文
    public static String getDayOfWeekInChinese(DayOfWeek dayOfWeek) {
        switch (dayOfWeek) {
            case MONDAY:
                return "星期一";
            case TUESDAY:
                return "星期二";
            case WEDNESDAY:
                return "星期三";
            case THURSDAY:
                return "星期四";
            case FRIDAY:
                return "星期五";
            case SATURDAY:
                return "星期六";
            case SUNDAY:
                return "星期日";
            default:
                return "";
        }
    }
//    @GetMapping(value = "/lay")
//    public List<FrontdeskNameCategory> t2(){
//        List<FrontdeskNameCategory> frontdeskNameCategories = frontdeskNameCategoryService.listTree();
//        return frontdeskNameCategories;
//    }
//    @GetMapping(value = "/exer")
//    public List<FrontdeskNameCategory> t3(){
//        List<FrontdeskNameCategory> frontdeskNameCategories = frontdeskNameCategoryService.getAllCategories();
//        return frontdeskNameCategories;
//    }
//根据id展示
    @GetMapping(value = "/displaydata/select")
    public Result display(@RequestParam Integer id){
        FrontdeskNameCategory selectcategorybyid = frontdeskNameCategoryService.selectcategorybyid(id);
        return Result.success(selectcategorybyid);
        //return frontdeskNameCategories;
    }

    //根据父id展示子id
    @GetMapping(value = "/seleccategorybyid")
    public Result selectnamebycategoryid(@RequestParam Integer id){
        List<Integer> selectcategorybyid = frontdeskNameCategoryService.selectnamebyid(id);
        return Result.success(selectcategorybyid);
        //return frontdeskNameCategories;
    }
}
